package com.flowable.demo;

import org.flowable.engine.DynamicBpmnService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.DeploymentBuilder;
import org.flowable.engine.runtime.ProcessInstance;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:flowable-context.xml")
public class ErrorEventDemo {

	private ProcessEngine processEngine;
	private TaskService taskService;
	private RuntimeService runtimeService;
	private RepositoryService repositoryService;
	private HistoryService historyService;
	private DynamicBpmnService dynamicBpmnService;
	private IdentityService identityService;
	private ManagementService managementService;
	private ProcessEngineConfiguration processEngineConfiguration;

	@Before
	public void testProcessEngine() {
		processEngine = ProcessEngines.getDefaultProcessEngine();
		System.out.println("流程引擎类：" + processEngine);

		taskService = processEngine.getTaskService();
		System.out.println(taskService);

		runtimeService = processEngine.getRuntimeService();
		System.out.println(taskService);

		repositoryService = processEngine.getRepositoryService();
		System.out.println(repositoryService);

		historyService = processEngine.getHistoryService();
		System.out.println(historyService);

		dynamicBpmnService = processEngine.getDynamicBpmnService();
		System.out.println(dynamicBpmnService);


		identityService = processEngine.getIdentityService();
		System.out.println(identityService);

		managementService = processEngine.getManagementService();
		System.out.println(managementService);

		String name = processEngine.getName();
		System.out.println("流程引擎的名称： " + name);

		processEngineConfiguration = processEngine.getProcessEngineConfiguration();
		System.out.println(processEngineConfiguration);

	}

	/**
	 * 关闭流程引擎
	 */
	@After
	public void close() {
		processEngine.close();
	}

	/**
	 * classpath方式部署 涉及三张表：ACT_RE_PROCDEF,ACT_RE_DEPLOYMENT,ACT_GE_BYTEARRAY
	 */
	@Test
	public void deploy() {
		DeploymentBuilder deploymentBuilder = repositoryService
				.createDeployment()
				.category("errorprocessevent")
				.key("errorprocessevent").name("错误事件处理")
				.addClasspathResource("错误事件处理.bpmn20.xml");
		Deployment deploy = deploymentBuilder.deploy();

		System.out.println("流程部署ID: " + deploy.getId());
	}
	/**
	 *启动流程实例
	 */
	@Test
	public void startProcessInstanceByKey() {
		String processDefinitionKey="errorprocessevent";
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

		System.out.println("流程ID: " + processInstance.getId());
	}
	/**
	 * 完成任务
	 */
	@Test
	public void complete() {
		String taskId = "135010";
		taskService.complete(taskId);
		
	}
	
}